<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Inventory_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
	}
		
	// =============================
	// = ========== GET ========== =
	// =============================
	function count_all_items()
	{
		$this->db->select('id');
		$this->db->from('item');
		//$this->db->where('status',0);
		return $this->db->count_all_results();
	}
	function get_items($offset,$page)
	{
		$return = null;
		$this->db->select('*');
		$this->db->from('item');		
		$this->db->limit($offset, $page);
		$query = $this->db->get();
		$return = $query->result_array();
		return $return;
	}
	function get_list_items()
	{
		$return = null;
		$this->db->select('*');
		$this->db->from('item');		
		$query = $this->db->get();
		$return = $query->result_array();
		return $return;
	}
	/*
	 * Thêm Item mới với status =0; trạng thái chưa submit
	 */
	function add_item()
	{
		$data = null;
		$this->db->set($data);
		$return = $this->db->insert('item');
		return $return;
	}
	
	function get_list_import_bills()
	{
		
		return null;
	}
	function add_import_bill()
	{
		return null;
	}
	function add_export_bill()
	{
		return null;
	}
	function get_list_export_bills()
	{
		return null;
	}
	
	function get_the_item($id)
	{
		$return = null;
		$this->db->select('*');
		$this->db->from('item');	
		$this->db->where('id=',$id);
		$query = $this->db->get();
		$return = $query->result_array();
		if(count($return)>0)
		{
			return $return[0];
		}
		return null;
	}
	/*
	 * Chuyển trạng thái status từ 0 tới 1
	 */
	function submitinventory($id)
	{
		$data = array
				(
					'status'=> 1
				);
		$this->db->where('id',$id);
		$return = $this->db->update('item',$data);
		return $return;
	}
	function edit_item($id)
	{
		$data = null;
		$this->db->where('id',$id);
		$return = $this->db->update('item',$data);
		return $return;
	}
	// --------------------------------------------------------------------
	
	/**
	 * Get all entries
	 *
	 * @access	public
	 * @return 	object	Entries
	 */
	function add_hocvien()
	{
		//$ngaysinh = ($this->input->post('ngaysinh') != '') ? date('Y-m-d', strtotime($this->input->post('ngaysinh'))) : date('Y-m-d');
		$date = str_replace('/', '-', $this->input->post('ngaysinh'));
		$ngaysinh = date('Y-m-d', strtotime($date));
		
		$date2 = str_replace('/', '-', $this->input->post('ngaycap'));
		$ngaycap = date('Y-m-d', strtotime($date2));
		
		//$malop = $this->get_malop($this->input->post('malop'));
		
		
		if($this->input->post('bodoi')==0)
		{
			$sothexuatngu = '';
			$date3 = str_replace('/', '-', $this->input->post('ngayxuatngu'));
			$ngayxuatngu = date('Y-m-d', strtotime($date3));
		}
		else
		{
			$sothexuatngu = $this->input->post('sothexuatngu');
			$date3 = str_replace('/', '-', $this->input->post('ngayxuatngu'));
			$ngayxuatngu = date('Y-m-d', strtotime($date3));
		}
		$data = array(
			//'mahv' 			=> $this->input->post('mahv'),
			'tenhv'			=> $this->input->post('tenhv'),
			'email'			=> $this->input->post('email'),
			'didong'		=> $this->input->post('didong'),
			'nguyenquan'	=> $this->input->post('nguyenquan'),
			'thuongtru'		=> $this->input->post('thuongtru'),
			'ngaysinh'		=> $ngaysinh,
			'gioitinh'		=> $this->input->post('gioitinh'),
			'hedaotao'		=> $this->input->post('hedaotao'),
			'id_lop'		=> $this->input->post('malop'),
			'cmnd'			=> $this->input->post('cmnd'),
			'ngaycap'		=> $ngaycap,
			'noicap'		=> $this->input->post('noicap'),
			'bodoi'			=> $this->input->post('bodoi'),
			'sothexuatngu'	=> $sothexuatngu,
			'ngayxuatngu'	=> $ngayxuatngu,
			'soquyetdinh'	=> $this->input->post('soquyetdinh'),
			'created_by'	=> $this->user->id,
			'status'		=> $this->input->post('status'),
		);
		
		$this->db->set($data);
		$this->db->insert('hocvien');
		
		$mat19 = 'T19-'.date('y').'-';
		$idhv = $this->db->insert_id();
		$mahv = $this->sinhmahv($idhv);
		
		$data2 = array(
			'mahv' => $mat19.$mahv,
		);
		
		$this->db->set($data2);
		$this->db->where('id', $idhv);
		$this->db->update('hocvien');
		
	}

}

/* Location: ./application/models/qlsv_model.php */
